﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace SimpleTerminal.Common
{
    public class D2d 
    {
        /*
        private readonly SharpDX.Direct2D1.Factory factory;
        private readonly SharpDX.DirectWrite.Factory writeFactory;
        private RenderTargetProperties renderProps;
        private HwndRenderTargetProperties hwndProps;
        private readonly WindowRenderTarget hwndRenderTarget;
        private bool disposedValue;

        public D2d(Control control) : this(control.Handle, control.Width, control.Height)
        {
        }



        public D2d(IntPtr Handle, int Width, int Height)
        {
            factory = new SharpDX.Direct2D1.Factory(SharpDX.Direct2D1.FactoryType.SingleThreaded);
            writeFactory = new SharpDX.DirectWrite.Factory();
            // 渲染参数。
            renderProps = new RenderTargetProperties()
            {
                PixelFormat = new PixelFormat(Format.Unknown, SharpDX.Direct2D1.AlphaMode.Premultiplied),
                Usage = RenderTargetUsage.None,
                Type = RenderTargetType.Default
            };
            // 渲染目标属性。
            hwndProps = new HwndRenderTargetProperties()
            {
                // 承载控件的句柄。
                Hwnd = Handle,
                // 控件的尺寸。
                PixelSize = new SharpDX.Size2(Width, Height),
                PresentOptions = PresentOptions.None
            };
            // 渲染目标。
            hwndRenderTarget = new WindowRenderTarget(factory, renderProps, hwndProps)
            {
                AntialiasMode = AntialiasMode.PerPrimitive
            };
            
            //hwndRenderTarget.FillRectangle
        }


        public void Resize(Size size)
        {
            hwndRenderTarget.Resize(new SharpDX.Size2(size.Width, size.Height));
        }

        public void Clear(Color color)
        {
            hwndRenderTarget.Clear(RawColor4(color));
        }

        public static SharpDX.Mathematics.Interop.RawColor4 RawColor4(Color color)
        {
            return new SharpDX.Mathematics.Interop.RawColor4(
                    color.R / 255f,
                    color.G / 255f,
                    color.B / 255f,
                    color.A / 255f
                    );
        }

        public TextFormat CreateTextFormat(System.Drawing.Font font)
        {
            return new TextFormat(writeFactory,
                font.Name,
                font.Bold ? FontWeight.Bold : FontWeight.Normal,
                font.Italic ? SharpDX.DirectWrite.FontStyle.Italic : SharpDX.DirectWrite.FontStyle.Normal,
                font.Size);

        }
        public SolidColorBrush CreateSolidColorBrush(Color color)
        {
            return new SolidColorBrush(hwndRenderTarget, D2d.RawColor4(color));
        }
        public void FillRectangle(SolidColorBrush solidColorBrush, Rectangle rectangle)
        {
            FillRectangle(solidColorBrush, rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);
        }
        public void FillRectangle(SolidColorBrush solidColorBrush, float x, float y, float width, float height)
        {
            hwndRenderTarget.FillRectangle(
                new SharpDX.Mathematics.Interop.RawRectangleF(
                    x,
                    y,
                    x + width,
                    y + height
                ),
                solidColorBrush);
        }
        /// <summary>
        /// 填充一个圆形
        /// </summary>
        /// <param name="solidColorBrush"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="radiusX"></param>
        /// <param name="radiusY"></param>
        public void FillEllipse(SolidColorBrush solidColorBrush, Point center, float radiusX, float radiusY)
        {
            hwndRenderTarget.FillEllipse(
                new Ellipse(new SharpDX.Mathematics.Interop.RawVector2(center.X, center.Y),
                radiusX, radiusY), solidColorBrush);
        }

        public void FillEllipse(SolidColorBrush solidColorBrush, Rectangle rectangle)
        {
            this.FillEllipse(solidColorBrush, new Point(
                rectangle.X + rectangle.Width / 2,
                rectangle.Y + rectangle.Height / 2), rectangle.Width / 2, rectangle.Height / 2);
        }
        public void DrawRectangle(SolidColorBrush solidColorBrush, Rectangle rectangle)
        {
            hwndRenderTarget.DrawRectangle(
                new SharpDX.Mathematics.Interop.RawRectangleF(rectangle.X, rectangle.Y, rectangle.Right, rectangle.Bottom),
                solidColorBrush
                );
        }

        public void DrawText(SolidColorBrush solidColorBrush, string text, TextFormat textFormat, Rectangle rectangle)
        {
            hwndRenderTarget.DrawText(text,
                textFormat,
                new SharpDX.Mathematics.Interop.RawRectangleF(
                    rectangle.X,
                    rectangle.Y,
                    rectangle.Right,
                    rectangle.Bottom
                ),
                solidColorBrush
                );
        }
        public void DrawText(SolidColorBrush solidColorBrush, string text, TextFormat textFormat, float x, float y, float width, float height)
        {
            hwndRenderTarget.DrawText(text,
                textFormat,
                new SharpDX.Mathematics.Interop.RawRectangleF(
                    x,
                    y,
                    x + width,
                    y + height
                ),
                solidColorBrush
                );
        }
        public void BeginDraw()
        {
            hwndRenderTarget.BeginDraw();
        }

        public void EndDraw()
        {
            hwndRenderTarget.EndDraw();
        }
        public void Flush()
        {
            hwndRenderTarget.Flush();

        }
        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    // TODO: 释放托管状态(托管对象)
                }

                // TODO: 释放未托管的资源(未托管的对象)并替代终结器
                // TODO: 将大型字段设置为 null
                hwndRenderTarget.Dispose();
                writeFactory.Dispose();
                factory.Dispose();
                disposedValue = true;
            }
        }

        // // TODO: 仅当“Dispose(bool disposing)”拥有用于释放未托管资源的代码时才替代终结器
        ~D2d()
        {
            // 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中
            Dispose(disposing: false);
        }

        public void Dispose()
        {
            // 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中
            Dispose(disposing: true);
            GC.SuppressFinalize(this);
        }

*/
    }
}
